﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using RAP.Framework.Libary.DataSource.Model;
using RAP.Framework.Libary.Utils;

namespace RAP.Framework.Libary.DataSource.DataReader
{
  /// <summary>
  /// 将结果集按指定字段的值转换成键值对
  /// </summary>
  public class KeyObjectParser : DataModelParser
  {
    /// <summary>
    /// 将DataReader当前结果集转换成一个键值对
    /// </summary>
    /// <param name="dataReader"></param>
    /// <param name="pickFields"></param>
    /// <param name="type">结果类型名称</param>
    /// <param name="parseContext">解析时用到的上下文对象</param>
    /// <returns></returns>
    public override ParseResult ParseData(IDataReader dataReader, IList pickFields, string type, DataParseContext parseContext)
    {
      var list = new ArrayList();
      var fields = this.GetFields(dataReader, pickFields);
      var paramArr = parseContext.ModelParams.Split(',');
      var keyProperty = paramArr.First<string>(string.Empty);
      var itemModelName = paramArr.Length > 1 ? paramArr[1] : string.Empty;
      var isGeneric = itemModelName.IsNotNullOrEmpty();
      if (isGeneric)
      {
        return ParseGenericData(dataReader, fields, keyProperty, itemModelName, parseContext);
      }
      else
      {
        while (dataReader.Read())
        {
          var data = CreateDataModel() as IDictionary;
          for (var i = 0; i < fields.Count; i++)
          {
            var field = fields[i];
            data.Add(field.Name, GetDBValue(dataReader, field));
          }
          list.Add(data);
        }

        return new ParseResult
        {
          DataValue = list.ToDictionaryObject(keyProperty),
          DataCount = list.Count
        };
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="resultTypeName"></param>
    /// <param name="properites"></param>
    /// <returns></returns>
    public override object CreateDataModel(string resultTypeName = null, IDictionary properites = null)
    {
      var data = new ListDictionary();
      if (properites.IsNotNullOrEmpty())
      {
        properites.CopyTo(data);
      }
      return data;
    }

    /// <summary>
    /// 将结果转换成指定值类型（泛型）的键值对
    /// </summary>
    /// <param name="dataReader"></param>
    /// <param name="fields"></param>
    /// <param name="keyProperty"></param>
    /// <param name="itemModelName"></param>
    /// <param name="parseContext"></param>
    /// <returns></returns>
    protected virtual ParseResult ParseGenericData(IDataReader dataReader, IList<DBField> fields, string keyProperty, string itemModelName, DataParseContext parseContext)
    {
      var itemModelType = GetDataModelType(itemModelName);
      var dictGeneric = typeof(Dictionary<,>).MakeGenericType(typeof(string), itemModelType).CreateInstance<IDictionary>();
      while (dataReader.Read())
      {
        var data = CreateDataModel(itemModelType);
        var setters = GetPropertyValueSetters(data);
        var keyValue = string.Empty;
        for (var i = 0; i < fields.Count; i++)
        {
          var field = fields[i];
          var val = GetDBValue(dataReader, field);
          if (field.Name == keyProperty)
          {
            keyValue = val.ToString();
          }
          SetPropertyValue(data, setters, field.Name, val);
        }
        dictGeneric.Add(keyValue, data);
      }
      return new ParseResult
      {
        DataValue = dictGeneric,
        DataCount = dictGeneric.Count
      };
    }
  }
}
